// pages/centerSafety/centerSafety.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userSafety:[],
    nowaddpwd:'',
    userName:'',
    nowusername:'',
    newusername:'',
  },
  onLoad:function(){
    //获取用户id发送请求
    var userid = wx.getStorageSync("OoOoUserid");
    var data = {
      userid:userid,
    }
    var that = this;
    //获取个人资料信息
    wx.request({
      url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/index_lwx',
      dataType:'json',
      method:'POST',
      data:data,
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
          that.setData({
            //第一个data为固定用法
            userSafety: res.data
          })
        },
        fail:function(err){
        console.log(err)
        }
    });
  },
  savepwdBtn:function(e){
    var that = this;
    if(e.detail.value.nowusername == ''){
      wx.showToast({
        title: '请输入当前密码',
        icon: 'none',
        duration: 2000
      })
      return
    }else if(e.detail.value.newusername == ''){
      wx.showToast({
        title: '请输入新密码',
        icon: 'none',
        duration: 2000
      })
      return
    }
    var nowaddpwd = e.detail.value.nowusername;
    var addpwd = e.detail.value.newusername;
    var userid = wx.getStorageSync("OoOoUserid");
    var data = {
      nowaddpwd : nowaddpwd,
      addpwd : addpwd,
      userid : userid,
    }

    wx.request({
      url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/updateUserPwd',
      dataType:'json',
      method:'POST',
      data:data,
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
          if(res.data == 10001){
            wx.showToast({
              title: '当前密码错误',
              icon: 'none',
              duration: 2000
            })
            return;
          }else if(res.data == 10000){
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            })

            that.onLoad()
            return;
          }
        },
        fail:function(err){
        console.log(err)
        }
    });
  }
})